﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MShop.Core.Domain;
using Castle.Core;

namespace MShop.Core.DataAccess
{
   
    public class ProductDao : CommonDao<Product>, IProductDao
    {
       
        private readonly IObjectContextProvider _Provider;
        public ProductDao(IObjectContextProvider provider):base(provider)
        {
            _Provider = provider;
        }

        public override void Delete(Product entity)
        {        
            base.Delete(entity.SkuPropImg);
            base.Delete(entity.Sku);
            base.Delete(entity.ProductSkuProp);
            base.Delete(entity.ProductProp);
            base.Delete(entity.CartItem);
            base.Delete(entity.ProductImg);
            base.Delete(entity);
        }

        public  virtual  IQueryable<Product> GetProducts(int? cid, decimal? minprice, decimal? maxprice, int? region)
        {
            var q = Find(x => x.IsPublish && x.IsDelete == false && x.Status == 1);
            if (region.HasValue && region != -1)
            {
                q = from item in q where item.Region == region select item;
            }
            if (cid.HasValue && cid != -1)
            {
                q = from item in q where item.CId == cid.Value select item;
            }
            if (minprice.HasValue && minprice != -1)
            {
                q = from item in q where item.ShopPrice >= minprice.Value select item;
            }
            if (maxprice.HasValue && maxprice != -1)
            {
                q = from item in q where item.ShopPrice <= maxprice.Value select item;
            }
            return q;
        }


        #region IProductDao Members


        public IQueryable<Product> GetValidProducts()
        {
            var q = Find(x => x.IsPublish && x.IsDelete == false && x.Status == 1);
            return q;
        }

        #endregion
    }
}